package org.casper.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.casper.bean.User;
import org.casper.dao.UserDao;

/**
 * Servlet implementation class RegisterServlet
 */
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private UserDao userDao = new UserDao();   
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		HttpSession session = request.getSession();
		/**
		 * 先验证验证码 对不对 不对就直接返回登录
		 */
		if(request.getParameter("code").equals(session.getAttribute("code"))){
			User newUser = new User(request.getParameter("username"), request.getParameter("userpass"));
			if(userDao.addUser(newUser)==1){
				response.sendRedirect("login.html");
			}else{
				response.sendRedirect("register.jsp");
			}
		}else{
			response.sendRedirect("register.jsp");
		}
		
	}

}
